package com.api.service;

import com.api.dao.UserDao;
import com.api.model.User;
import com.api.utils.BeanHelper;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    public User login(User query) {
       return userDao.login(query);

    }
   /* @Autowired
    private UserDao userDao;

    @Autowired
    private FileService fileService;


    @Value("${domain.name}")
    private String domainName;

    public String getusername(Long id){
        return userDao.getusername(id);
    }

    public boolean isExist(String email) {
        return getUser(email) !=null;
    }

    private User getUser(String email) {
        User queryUser=new User();
        queryUser.setEmail(email);
       List<User> users= getUserByQuery(queryUser);
       if(!users.isEmpty()){
           return users.get(0);
       }
       return null;
    }

    private List<User> getUserByQuery(User queryUser) {
        List<User> users=userDao.getUserList(queryUser);
        return  users;
    }

    public boolean addAccount(User account) {
        if(account.getAvatarFile()!=null){
            List<String> images = fileService.getImgPaths(Lists.newArrayList(account.getAvatarFile()));
            account.setAvatar(images.get(0));
        }
        account.setEnableUrl("http://"+domainName+"/accounts/verify");
        BeanHelper.setDefaultProp(account,User.class);
        userDao.addUser(account);
        return true;

    }*/

   /* public boolean enable(String key) {
        String email = redisTemplate.opsForValue().get(key);
        if (StringUtils.isBlank(email)) {
            throw new UserException(UserException.Type.USER_NOT_FOUND, "无效的key");
        }
        User updateUser = new User();
        updateUser.setEmail(email);
        updateUser.setEnable(1);
        userMapper.update(updateUser);
        return true;
    }*/
}
